package com.config;

import com.interceptor.CorsInterceptor;
import com.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    /**
     * 注入跨域拦截器
     * @return
     */
   @Bean
    CorsInterceptor corsInterceptor(){
       return new CorsInterceptor();
   }

    /**
     * 注入登录拦截器
     * @return
     */
   @Bean
    LoginInterceptor loginInterceptor(){
       return new LoginInterceptor();
   }

    /**
     * 跨域拦截所有请求
     * 登录拦截配置全部api/v1/pri/user/**,除了登录和注册请求
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(corsInterceptor()).addPathPatterns("/**");

       registry.addInterceptor(loginInterceptor()).addPathPatterns("api/v1/pri")
               .excludePathPatterns("api/v1/pri/user/login","api/v1/pri/user/register");

       WebMvcConfigurer.super.addInterceptors(registry);
    }
}
